package com.wechat.wins.http;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Random;

import javax.crypto.Cipher;

import com.wechat.wins.util.CertifcateUtils;

public class HttpsBase {

    protected static PublicKey publicKey;
    protected static PrivateKey privateKey;

    static {

    }

    /**
     * 对数据解密
     * @param data
     * @param seed
     * @return decrypt data
     * @throws Exception
     */
    public static byte[] decrypt(final byte[] data, final SecureRandom seed) throws Exception {
        final Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
        if (seed != null) {
            cipher.init(Cipher.DECRYPT_MODE, privateKey, seed);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
        }
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(final byte[] data) throws Exception {
        return decrypt(data, null);
    }

    /**
     * 对数据加密
     * @param data
     * @param seed
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(final byte[] data, final SecureRandom seed)
            throws Exception {
        if (publicKey == null) {
            publicKey = CertifcateUtils.readPublicKeys();
        }
        final Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
        if (seed != null) {
            cipher.init(Cipher.ENCRYPT_MODE, publicKey, seed);
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        }
        return cipher.doFinal(data);
    }

    public static byte[] encrypt(final byte[] data) throws Exception {
        return encrypt(data, null);
    }

    public static String byte2hex(final byte[] b) {
        String hs = "";
        String stmp = "";
        for (final byte aB : b) {
            stmp = (Integer.toHexString(aB & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + "  " + stmp;
            }
        }
        return hs.toUpperCase();
    }

    public static byte[] cactHash(final byte[] bytes) {
        byte[] _bytes = null;
        try {
            final MessageDigest md = MessageDigest.getInstance("SHA1");
            md.update(bytes);
            _bytes = md.digest();
        } catch (final NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return _bytes;
    }


    protected static String random() {
        final StringBuilder builder = new StringBuilder();
        final Random random = new Random();
        final int seedLength = 10;
        for (int i = 0; i < seedLength; i++) {
            builder.append(digits[random.nextInt(seedLength)]);
        }

        return builder.toString();
    }

    private static final char[] digits = {
            '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e',
            'f', 'g', 'h', 'i', 'j'
    };

}
